<?php

/**
 * 订单预售服务
 */
namespace App\Service\OrderService\OrderInterfaceService;
use App\Service\BaseService;
use App\Service\Code\Normal;
use App\Service\AppService\SystemCacheConfigService;
use App\Model\PssModel\OrderSupplierAmountExtra;
use App\Model\PssModel\OrderRefundGoods;
use App\Model\PssModel\OrderSupplier;
class OrderPreSellService extends BaseService
{
    /**
     * 处理供应商订单价格服务
     * @param array $order
     * @param array $supplierOrder
     * @param array $orderGoods
     */
    public function getOrderSupplierPrice(array $order,array $supplierOrder,array $orderGoods)
    {
        //尾款信息
        $final_money                = $order['final_money'];
        $final_thirdparty_money     = $order['final_thirdparty_money'];
        $final_advance_money        = $order['final_advance_money'];
        $final_points_money         = $order['final_points_money'];
        $final_red_envelope_money   = $order['final_red_envelope_money'];
        $final_payway_money         = $order['final_payway_money'];
        $final_points               = $order['final_points'];
        $_goods_money               = $supplierOrder['goods_money'];
        $deposit_delivery_money     = $supplierOrder['deliver_money'];
        $deposit_goods_money        = bcsub($supplierOrder['real_total_money'],$deposit_delivery_money,2);
        
        //分摊尾款信息到商品行
        $has_final_money                = '0.00';
        $has_final_thirdparty_money     = '0.00';
        $has_final_advance_money        = '0.00';
        $has_final_points_money         = '0.00';
        $has_final_red_envelope_money   = '0.00';
        $has_final_payway_money         = '0.00';
        $has_final_activity_money       = '0.00';
        $has_final_points               = '0.00';
        $has_goods_money                = '0.00';
        $has_delivery_money             = '0.00';
        $total_goods_money              = '0.00';
        $total_discount_money           = '0.00';
        $new_goods_list = [];
        $count = count($orderGoods);
        $total_goods_real_price  = '0.00';
        foreach ($orderGoods as $key => $goods) 
        {
            $deposit_discount_activity_money = $goods['discount_activity_money'];
            $price = $goods['price'];
            $quantity = $goods['quantity'];
            $total_price = bcmul($price,$quantity,2);
            if( $count == $key + 1 ) 
            {
                //预付卡
                $discount_precard_money         = bcsub($final_advance_money,$has_final_advance_money,2);
                //积分抵扣金额
                $discount_point_money           = bcsub($final_points_money,$has_final_points_money,2);
                //红包抵扣金额
                $discount_red_envelope_money    = bcsub($final_red_envelope_money,$has_final_red_envelope_money,2);
                //支付方式优惠金额
                $discount_payway_money          = bcsub($final_payway_money,$has_final_payway_money,2);
                //积分抵扣
                $discount_points                = bcsub($final_points,$has_final_points,2);
                //商品实际付款
                $discount_goods_money           = bcsub($deposit_goods_money,$has_goods_money,2);
                //运费
                $discount_freight_money         = bcsub($deposit_delivery_money,$has_delivery_money,2);
                //反算分摊优惠定金多出来的金额
                $discount_final_money           = bcsub($final_money,$has_final_money,2);
            }
            else
            {
                //商品总金额
                $scale_price                    = bcdiv($total_price,$_goods_money,4);
                //预付卡
                $discount_precard_money         = bcmul($final_advance_money,$scale_price,2);
                //积分抵扣金额
                $discount_point_money           = bcmul($final_points_money,$scale_price,2);
                //红包抵扣金额
                $discount_red_envelope_money    = bcmul($final_red_envelope_money,$scale_price,2);
                //支付方式优惠金额
                $discount_payway_money          = bcmul($final_payway_money,$scale_price,2);
                //积分抵扣
                $discount_points                = bcmul($final_points,$scale_price,2);
                //商品实际付款
                $discount_goods_money           = bcmul($deposit_goods_money, $scale_price,2);
                //运费
                $discount_freight_money         = bcmul($deposit_delivery_money,$scale_price,2);
                //反算分摊优惠定金多出来的金额，分摊优惠金额
                $discount_final_money           = bcmul($final_money,$scale_price,2);
                //累加已分摊数据
                $has_final_money                = bcadd($has_final_money,$discount_final_money,2);
                $has_final_advance_money        = bcadd($has_final_advance_money,$discount_precard_money,2);
                $has_final_points               = bcadd($has_final_points,$discount_points,2);
                $has_final_points_money         = bcadd($has_final_points_money,$discount_point_money,2);
                $has_final_red_envelope_money   = bcadd($has_final_red_envelope_money,$discount_red_envelope_money,2);
                $has_final_payway_money         = bcadd($has_final_payway_money,$discount_payway_money,2);
                $has_goods_money                = bcadd($has_goods_money,$discount_goods_money,2);
                $has_delivery_money             = bcadd($has_delivery_money,$discount_freight_money,2);
            }
            
            //最新活动金额
            $discount_activity_money             = bcsub($deposit_discount_activity_money,$discount_final_money,2);
            //定金商品金额
            $discount_goods_money                = bcsub((string)$total_price,(string)$goods['discount_money'],2);
            //组装新的商品优惠
            $temp_goods =
            [
                'dataId'                            => $goods['id'],
                'discount_red_envelope_money'       => bcadd($goods['discount_red_envelope_money'],$discount_red_envelope_money,2),
                'discount_payway_money'             => bcadd($goods['discount_payway_money'],$discount_payway_money,2),
                'discount_precard_money'            => bcadd($goods['discount_precard_money'],$discount_precard_money,2),
                'discount_point_money'              => bcadd($goods['discount_point_money'],$discount_point_money,2),
                'discount_points'                   => bcadd($goods['discount_points'],$discount_points,2),
                'discount_activity_money'           => $discount_activity_money,
                'discount_coupon_money'             => $goods['discount_coupon_money'],
                'discount_payway_money'             => $goods['discount_payway_money'],
                'discount_deposit_goods_money'      => $discount_goods_money,
                'discount_deposit_delivery_money'   => $discount_freight_money,
            ];
            
            //计算总的优惠金额
            $single_goods_discount_money = bcadd($temp_goods['discount_red_envelope_money'],$temp_goods['discount_precard_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_point_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_payway_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_activity_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_coupon_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_payway_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_deposit_goods_money'],2);
            $single_goods_discount_money = bcadd($single_goods_discount_money,$temp_goods['discount_deposit_delivery_money'],2);
            

            //总优惠
            $total_discount_money                       = bcadd($total_discount_money, $single_goods_discount_money,2);
            
            //商品金额+运费
            $single_goods_total                         = bcadd($total_price,$discount_freight_money,2);
            //商品总金额+尾款包邮
            $total_goods_money                          = bcadd($total_goods_money,$single_goods_total,2);
            //开始重新赋值
            //单品运费
            $single_goods_freight                       = bcdiv($discount_freight_money,$quantity,2);
            $goods['price']                             = bcadd($single_goods_freight,$price,2);
            $goods['freight']                           = '0.00';
            $goods['discount_money']                    = $single_goods_discount_money;
            $goods['discount_red_envelope_money']       = $temp_goods['discount_red_envelope_money'];
            $goods['discount_payway_money']             = $temp_goods['discount_payway_money'];
            $goods['discount_activity_money']           = $temp_goods['discount_activity_money'];
            $goods['discount_payway_money']             = $temp_goods['discount_payway_money'];
            $goods['discount_precard_money']            = $temp_goods['discount_precard_money'];
            $goods['discount_point_money']              = $temp_goods['discount_point_money'];
            $goods['discount_points']                   = $temp_goods['discount_points'];
            $goods['discount_deposit_goods_money']      = $discount_goods_money;
            $goods['discount_deposit_delivery_money']   = $discount_freight_money;
            
            //给到新数组
            $new_goods_list[] = $goods;
        }
        
        $supplierOrder['pay_money']         = $final_thirdparty_money;
        $supplierOrder['order_money']       = $total_goods_money;
        $supplierOrder['deliver_money']     = '0.00';
        $supplierOrder['discount_money']    = $total_discount_money;
        $supplierOrder['total_money']       = $total_goods_money;
        $supplierOrder['real_total_money']  = $final_thirdparty_money;
        return [
            'supplierOrder' => $supplierOrder,
            'orderGoods'    => $new_goods_list,
        ];
    }
    
    /**
     * 检查预付款订单金额是否正常
     * @param array $order
     * @param array $supplierOrder
     * @param array $order_amount_extra
     * //订单金额=定金+尾款=定金三方支付+定金预付款+定金积分支付+尾款三方支付+尾款预付款+尾款定金支付+尾款红包支付
     */
    public function checkDepositMoney(array $order,array $supplierOrder,array $order_amount_extra)
    {
        if( empty($order['payMode']) ) {
            return Normal::returnCode(Normal::FAIL,"预售订单付款模式payMode不能为空");
        }
    
        if( empty(OrderPay::instance()->pay_mode[$order['payMode']]) ) {
            return Normal::returnCode(Normal::FAIL,"预售订单付款模式payMode不正确,【1-正常全款（默认），2-定金支付，3-尾款支付】");
        }
        
        $payMode = $order['payMode'];
        //订单商品类型
        $order_commodity_type = $supplierOrder['order_commodity_type'];
        //实际三方支付申报金额
        $real_total_money     = (string)$supplierOrder['real_total_money'];
        $update_amount_extra = [];
        if( $payMode == OrderPay::PAY_MODE_DEPOSIT )//定金支付
        {
            if( empty($order['deposit_sn'])) {
                return Normal::returnCode(Normal::FAIL,"定金或全款支付单号deposit_sn不能为空");
            }
    
            if( !isset($order['deposit_money'])) {
                return Normal::returnCode(Normal::FAIL,"定金订单金额deposit_money不能为空");
            }
    
            if( !isset($order['deposit_thirdparty_money'])) {
                return Normal::returnCode(Normal::FAIL,"定金三方支付金额deposit_thirdparty_money不能为空");
            }
    
            if( !isset($order['deposit_advance_money'])) {
                return Normal::returnCode(Normal::FAIL,"定金预付款金额deposit_advance_money不能为空");
            }
    
            if( !isset($order['deposit_points_money'])) {
                return Normal::returnCode(Normal::FAIL,"定金积分支付金额deposit_points_money不能为空");
            }
    
            if( !isset($order['deposit_activity_money'])) {
                return Normal::returnCode(Normal::FAIL,"定金支付剩余尾款当做活动金额deposit_activity_money不能为空");
            }
    
    
            if( !isset($order['deposit_payway_money'])) {
                //return Normal::returnCode(Normal::FAIL,"定金支付部分支付方式优惠金额deposit_payway_money不能为空");
            }
    
            if( !isset($order['deposit_points'])) {
                return Normal::returnCode(Normal::FAIL,"定金积分deposit_points不能为空");
            }
    
            if( !isset($order['deposit_pay_way'])) {
                return Normal::returnCode(Normal::FAIL,"定金三方支付方式deposit_pay_way不能为空");
            }
    
            $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('bd_crm_pay_'.$order['deposit_pay_way']);
            if( empty($config) ) {
                return Normal::returnCode(Normal::FAIL,"云仓中台-系统管理-业务配置-百大支付方式{$order['deposit_pay_way']}-微信对应百大CRM参数未配置");
            }
    
            //定金支付金额 = 定金三方支付金额 + 定金预付款金额 + 定金积分支付金额 + 支付方式优惠金额
            $deposit_money              = (string)$order['deposit_money'];
            $deposit_thirdparty_money   = (string)$order['deposit_thirdparty_money'];
            $deposit_advance_money      = (string)$order['deposit_advance_money'];
            $deposit_points_money       = (string)$order['deposit_points_money'];
            $deposit_points             = (string)$order['deposit_points'];
            $deposit_activity_money     = (string)$order['deposit_activity_money'];
            $deposit_payway_money       = (string)($order['deposit_payway_money'] ?? '0.00');
    
            //定金总金额
            $discount_discount_money    = bcadd((string)$deposit_advance_money,$deposit_points_money,2);
            $discount_discount_money    = bcadd((string)$discount_discount_money,$deposit_activity_money,2);
            $discount_discount_money    = bcadd((string)$discount_discount_money,$deposit_payway_money,2);
            $money = bcadd((string)$deposit_thirdparty_money,(string)$discount_discount_money,2);
            if( $deposit_money < 0 ) {
                return Normal::returnCode(Normal::FAIL,"预售订单定金模式定金总金额deposit_money不能小于0");
            }
    
            if( bccomp($money,$deposit_money,2) != 0 ) {
                return Normal::returnCode(Normal::FAIL,"预售订单定金模式(三方支付金额deposit_thirdparty_money + 预付卡金额deposit_advance_money + 积分支付金额deposit_points_money + 定金支付尾款优惠金额deposit_activity_money + 定金支付方式优惠金额deposit_payway_money)合计不等于定金总金额deposit_money");
            }
    
            if( bccomp($deposit_thirdparty_money,$real_total_money,2) != 0 ) {
                return Normal::returnCode(Normal::FAIL,"预售订单定金模式，三方线上用户实际支付金额deposit_thirdparty_money不等于创建订单orderMoney实际支付总金额!");
            }
            
            $update_amount_extra = [
                'deposit_mode'              => 2,
                'order_pay_sn'              => $order['order_pay_sn'],
                'deposit_sn'                => $order['deposit_sn'],
                'deposit_pay_sn'            => $order['deposit_pay_sn'],
                'deposit_money'             => $deposit_money,
                'deposit_activity_money'    => $deposit_activity_money,
                'deposit_payway_money'      => $deposit_payway_money,
                'deposit_thirdparty_money'  => $deposit_thirdparty_money,
                'deposit_advance_money'     => $deposit_advance_money,
                'deposit_points_money'      => $deposit_points_money,
                'deposit_points'            => $deposit_points,
                'deposit_pay_way'           => $order['deposit_pay_way'],
                'deposit_pay_time'          => time(),
            ];
        }
        else
        {
            //判断定金是否发起售后
            $is_refund = OrderRefundGoods::instance()->getOrderIsRefund($order_amount_extra['order_id']);
            if( $is_refund) {
                return Normal::returnCode(Normal::FAIL,"订单已发起售后不能再进行支付尾款了!");
            }
            
            if( bccomp($order_amount_extra['deposit_money'],'0.00',2) == 0 ) {
                return Normal::returnCode(Normal::FAIL,"定金还未支付");
            }
            
            if( empty($order['final_sn'])) {
                return Normal::returnCode(Normal::FAIL,"尾款支付单号final_sn不能为空");
            }
    
            if( !isset($order['final_money'])) {
                return Normal::returnCode(Normal::FAIL,"尾款支付金额final_money不能为空");
            }
    
            if( !isset($order['final_thirdparty_money'])) {
                return Normal::returnCode(Normal::FAIL,"尾款三方支付金额final_thirdparty_money不能为空");
            }
            
            if( !isset($order['final_payway_money'])) {
                return Normal::returnCode(Normal::FAIL,"尾款支付方式优惠金额final_payway_money不能为空");
            }
    
            if( !isset($order['final_advance_money'])) {
                return Normal::returnCode(Normal::FAIL,"尾款预付款金额final_advance_money不能为空");
            }
    
            if( !isset($order['final_points_money'])) {
                return Normal::returnCode(Normal::FAIL,"尾款积分支付金额final_points_money不能为空");
            }
    
            if( !isset($order['final_points'])) {
                return Normal::returnCode(Normal::FAIL,"尾款积分final_points不能为空");
            }
    
            if( !isset($order['final_red_envelope_money'])) {
                return Normal::returnCode(Normal::FAIL,"尾款红包支付金额final_red_envelope_money不能为空");
            }
    
            if( !isset($order['final_pay_way'])) {
                //return Normal::returnCode(Normal::FAIL,"尾款支付方式final_pay_way不能为空");
            }
    
            $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('bd_crm_pay_'.$order['final_pay_way']);
            if( empty($config) ) {
                return Normal::returnCode(Normal::FAIL,"云仓中台-系统管理-业务配置-百大支付方式{$order['final_pay_way']}-微信对应百大CRM参数未配置");
            }
    
            //尾款支付金额 = 尾款三方支付金额 + 尾款预付款金额 + 尾款积分支付金额 + 尾款红包支付金额 + 尾款支付方式优惠金额
            $final_money              = (string)$order['final_money'];
            $final_thirdparty_money   = (string)$order['final_thirdparty_money'];
            $final_advance_money      = (string)$order['final_advance_money'];
            $final_points_money       = (string)$order['final_points_money'];
            $final_red_envelope_money = (string)$order['final_red_envelope_money'];
            $final_payway_money       = (string)($order['final_payway_money'] ?? '0.00');
            $final_points             = (string)$order['final_points'];
    
            $money = bcadd((string)$final_thirdparty_money,(string)$final_advance_money,2);
            $money = bcadd((string)$money,(string)$final_points_money,2);
            $money = bcadd((string)$money,(string)$final_red_envelope_money,2);
            $money = bcadd((string)$money,(string)$final_payway_money,2);
    
            //尾款支付金额
            if( $final_money < 0 ) {
                return Normal::returnCode(Normal::FAIL,"预售订单尾款模式总金额final_money不能小于0");
            }
    
            if( bccomp($money,$final_money,2) != 0 ) {
                return Normal::returnCode(Normal::FAIL,"预售订单尾款模式(三方支付金额final_thirdparty_money + 预付卡金额final_advance_money + 积分支付金额final_points_money + 红包支付金额final_red_envelope_money + 尾款支付方式优惠金额final_payway_money)合计不等于尾款总金额final_money");
            }
    
            //跨境保税订单，线上三方支付金额验证
            if( $order_commodity_type == 1 && bccomp($final_thirdparty_money,'0.00',2) == 0 ) {
                return Normal::returnCode(Normal::FAIL,"保税跨境预售订单尾款三方支付金额final_thirdparty_money不能等于0");
            }
            
            //判断尾款金额是否正确
            $deposit_activity_money = $order_amount_extra['deposit_activity_money'];
            if( bccomp($deposit_activity_money,$final_money,2) != 0 ) {
                return Normal::returnCode(Normal::FAIL,"预售订单尾款支付金额不正确，错误金额:{$final_money},正确尾款总金额:{$deposit_activity_money}");
            }
    
            //更新尾款数据
            $update_amount_extra = [
                'final_sn'                  => $order['final_sn'],
                'final_pay_sn'              => $order['final_pay_sn'],
                'final_pay_mode'            => 1,
                'final_money'               => $final_money,
                'final_thirdparty_money'    => $final_thirdparty_money,
                'final_advance_money'       => $final_advance_money,
                'final_points_money'        => $final_points_money,
                'final_points'              => $final_points,
                'final_red_envelope_money'  => $final_red_envelope_money,
                'final_payway_money'        => $final_payway_money,
                'final_pay_way'             => $order['final_pay_way'],
                'final_pay_time'            => time(),
            ];
        }
        
        return Normal::returnCode(Normal::SUC,"校验完成",[
            'payMode'               => $payMode,
            'update_amount_extra'   => $update_amount_extra
        ]);
    }
    
    /**
     * 查询是否只付了定金
     * @param string $order_supplier_id
     */
    public function isDepositMoneyOnly($order_supplier_id) 
    {
        $_extra_amount = OrderSupplierAmountExtra::instance()->info(['order_id'=>$order_supplier_id]);
        return $_extra_amount['deposit_mode'] == 2 && bccomp($_extra_amount['deposit_money'],'0.00',2) > 0 && bccomp($_extra_amount['final_money'],'0.00',2) == 0 ? true : false;
    }
    
    /**
     * 查询是否是预售非全款模式
     * @param string $order_supplier_id
     */
    public function isPreSellNoneAllPayMoneyOrder(array $supplierOrder,array $_extra_money)
    {
        return $supplierOrder['order_trade_type'] == OrderSupplier::order_trade_type_deposit && $_extra_money['deposit_mode'] == 2 ;
    }
    
    /**
     * 查询是否预售订单
     * @param string $order_supplier_id
     */
    public function isPreSellOrder(array $supplierOrder)
    {
        return $supplierOrder['order_trade_type'] == OrderSupplier::order_trade_type_deposit;
    }
}
